package net.zjitc.hashset.ktv;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @Author admin
 * @Date 2019/4/3 8:15
 */
public class SongDaoImp implements SongDao {

     ArrayList<Song>arrayList=new ArrayList<Song>();
    @Override
    public void init() {
        Song song1=new Song("夜曲");
        Song song2=new Song("稻香");
        Song song3=new Song("快乐的小青蛙");
        Song song4=new Song("小幸运");
        arrayList.add(song1);
        arrayList.add(song2);
        arrayList.add(song3);
        arrayList.add(song4);
        System.out.println("初始化歌曲列表：");
        Iterator iterator=arrayList.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
        System.out.println();
    }
@Override
    public void add(Song song) {
       arrayList.add(song);
    System.out.println("已添加歌曲："+song.getName());
    }
@Override
    public void all() {
      System.out.println("当前歌曲列表：");
      Iterator iterator=arrayList.iterator();
      while (iterator.hasNext()){
        Song song=(Song)iterator.next();
        System.out.println(song.getName()+"\t");
      }
     System.out.println();
    }
    /**
     *
     * */
@Override
    public void top(String name) {
        Song song=select(name);
        if (song==null){
            System.out.println("当前歌曲列表没有此歌曲");
        }else {
               arrayList.remove(song);
               arrayList.add(0,song);
               System.out.println("已将歌曲置顶！");

        }
    }
@Override
    public Song select(String name) {
    for (int i=0;i<arrayList.size();i++){
         if (arrayList.get(i).equals(name)){
             return arrayList.get(i);
         }
    }
        return null;
    }
@Override
    public void before(Song music) {
       int ind=arrayList.indexOf(music);
       arrayList.remove(music);
       arrayList.add(ind-1,music);

       }
    }

